package com.nimitz.modules.approval.controller;


import com.nimitz.common.core.constant.UserConstants;
import com.nimitz.common.core.web.controller.BaseController;
import com.nimitz.common.core.web.domain.AjaxResult;
import com.nimitz.modules.approval.domain.ApprovalProcedureProcess;
import com.nimitz.modules.approval.service.IApprovalProcedureProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ApprovalProcedureProcessController
 * @Author FuCun
 * @Date 2021-08-17-14:45
 * @PackageName com.nimitz.modules.approval.controller
 * @Description 审批流线程
 * @Version 1.0.0
 */
@RestController
@Api(tags = "审批流线程管理")
@RequestMapping("/approval-procedure-process")
public class ApprovalProcedureProcessController extends BaseController {

    @Autowired
    private IApprovalProcedureProcessService approvalProcedureProcessService;

    /**
     * 查询所有审批流线程（含搜索）
     *
     * @param  approvalProcedureProcess 审批流线程
     * @return 审批意见集合
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有审批流节点明细（含搜索）")
    public AjaxResult list(ApprovalProcedureProcess approvalProcedureProcess){
        List<ApprovalProcedureProcess> approvalProcedureProcesses=approvalProcedureProcessService.selectApprovalProcedureProcessList(approvalProcedureProcess);
        return AjaxResult.success(approvalProcedureProcesses);
    }

    /**
     * 新增审批流线程
     *
     * @param approvalProcedureProcess 审批流线程
     * @return 结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增审批流线程")
    public AjaxResult add(@RequestBody ApprovalProcedureProcess approvalProcedureProcess) {
        if (UserConstants.NOT_UNIQUE.equals(approvalProcedureProcessService.checkApprovalProcedureNoUnique(approvalProcedureProcess.getProcedureNo()))) {
            return AjaxResult.error("新增审批流节点明细信息'" + approvalProcedureProcess.getProcedureNo() + "'失败，审批流编号已存在");
        }
        //该处的创建者应该为登录用户的名称，暂时写死在这里。
        approvalProcedureProcess.setProcedureNo(1l);
        return toAjax(approvalProcedureProcessService.insertApprovalProcedureProcess(approvalProcedureProcess));
    }

    /**
     * 修改审批流线程
     *
     * @param approvalProcedureProcess 审批流线程
     * @return 结果
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改审批流线程")
    public AjaxResult edit(@RequestBody ApprovalProcedureProcess approvalProcedureProcess){
        if (UserConstants.NOT_UNIQUE.equals(approvalProcedureProcessService.checkApprovalProcedureNoUnique(approvalProcedureProcess.getProcedureNo()))) {
            return AjaxResult.error("新增审批流节点明细信息'" + approvalProcedureProcess.getProcedureNo() + "'失败，审批流编号已存在");
        }
        //该处的创建者应该为登录用户的名称，暂时写死在这里。
        approvalProcedureProcess.setProcedureNo(1l);
        return toAjax(approvalProcedureProcessService.updateApprovalProcedureProcess(approvalProcedureProcess));
    }

    /**
     * 删除审批流线程
     *
     * @param procedureProcessId 审批流节点id
     * @return 结果
     */
    @DeleteMapping("/{procedureProcessId}")
    @ApiOperation(value = "删除审批流线程")
    public AjaxResult remove(@PathVariable Long procedureProcessId) {
        return toAjax(approvalProcedureProcessService.deleteApprovalProcedureProcessByProcedureProcessId(procedureProcessId));
    }
}
